import ddddocr
from re import sub

class CaptchaRecognizer:
    def __init__(self):
        self.ocr = ddddocr.DdddOcr(show_ad=False)
    
    def recognize(self, image_bytes):
        """识别验证码图片"""
        try:
            result = self.ocr.classification(image_bytes)
            # 使用正则表达式去除所有非字母数字字符
            return sub(r'[^a-zA-Z0-9]', '', result)
        except Exception as e:
            print(f"验证码识别失败: {e}")
            return None